package me.toalec.RankUp;

import me.toalec.RankUp.Permissions.PermissionsManager;
import me.toalec.RankUp.util.Utility;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerListener;

public class UtilityCommand extends PlayerListener implements CommandExecutor{

	UtilityCommand(RankUp instance, ConfigManager config, PermissionsManager pm){
		this.config = config;
		this.pm = pm;
		
		helpMenu = new String[]{
				"V" + instance.getDescription().getVersion(),
			    "/ru enable <command|word> [package]",
			    "/ru disable <command|word> [package]",
			    "/ru setword <word> [package]",
			    "/ru setstarting <Default rank> [package]",
			    "/ru setending <GreyListed rank> [package]",
			    "/ru addpackage <Package Name>",
			    "/ru removepackage <Package Name>"
				};
	}
	
	@Override
	public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
		
		if(args.length >= 2){
			String packageNode;
			if(args.length == 2){
				packageNode = "default";
			}else if(args.length == 3){
				packageNode = args[2];
			}else{
				if(hasCommandPermission(sender, "rankup.ru"))
					displayHelp(sender);
				return true;
			}
			
			if(config.getAllPackageNames().contains(packageNode)){
				if(args[0].equalsIgnoreCase("enable") && hasCommandPermission(sender, "rankup.ru.enable")){
					if(args[1].equals("command") || args[1].equals("word")){
						if(!config.getBoolean(packageNode + ".rankup_" + args[1] + "_enabled")){
							if(config.setValue(packageNode + ".rankup_" + args[1] + "_enabled", true)){
								messageUnknown(sender, "The " + args[1] + " in package " + packageNode + " was enabled.");
							}else{
								messageUnknown(sender, "The " + args[1] + " in package " + packageNode + " was not enabled.");
							}
						}else{
							messageUnknown(sender, "The " + args[1] + " in package " + packageNode + " was already enabled.");
						}
					}
				}else if(args[0].equalsIgnoreCase("disable") && hasCommandPermission(sender, "rankup.ru.disable")){
					if(args[1].equals("command") || args[1].equals("word")){
						if(config.getBoolean(packageNode + ".rankup_" + args[1] + "_enabled")){
							if(config.setValue(packageNode + ".rankup_" + args[1] + "_enabled", false)){
								messageUnknown(sender, "The " + args[1] + " in package " + packageNode + " was disabled.");
							}else{
								messageUnknown(sender, "The " + args[1] + " in package " + packageNode + " was not disabled.");
							}
						}else{
							messageUnknown(sender, "The " + args[1] + " in package " + packageNode + " was already disabled.");
						}
					}
				}else if(args[0].equalsIgnoreCase("setword") && hasCommandPermission(sender, "rankup.ru.setword")){
					if(config.setValue(packageNode + ".rankup_word", args[1]))
						messageUnknown(sender, "The word in package " + packageNode + " has been changed to " + args[1]);
					else
						messageUnknown(sender, "The word in package " + packageNode + " was not able to change.");
				}else if(args[0].equalsIgnoreCase("setstarting") && hasCommandPermission(sender, "rankup.ru.setstarting")){
					if(config.setValue(packageNode + ".startingGroup", args[1]))
						messageUnknown(sender, "The starting Group in package " + packageNode + " has been changed to " + args[1]);
					else
						messageUnknown(sender, "The starting Group in package " + packageNode + " was not able to change.");
				}else if(args[0].equalsIgnoreCase("setending") && hasCommandPermission(sender, "rankup.ru.setending")){
					if(config.setValue(packageNode + ".endingGroup", args[1]))
						messageUnknown(sender, "The ending Group in package " + packageNode + " has been changed to " + args[1]);
					else
						messageUnknown(sender, "The ending Group in package " + packageNode + " was not able to change.");
				}else if(args[0].equalsIgnoreCase("addpackage") && hasCommandPermission(sender, "rankup.ru.addpackage")){
					if(!config.getAllPackageNames().contains(args[1])){
						config.addPackage(args[1]);
						messageUnknown(sender, "You successfully added package: " + args[1]);
					}else{
						messageUnknown(sender, "That package already exists.");
					}
				}else if(args[0].equalsIgnoreCase("removepackage") && hasCommandPermission(sender, "rankup.ru.removepackage")){
					if(config.getAllPackageNames().contains(args[1])){
						if(!args[1].equals("default")){
							config.removePackage(args[1]);
							messageUnknown(sender, "You successfully remove package: " + args[1]);
						}else{
							messageUnknown(sender, "You can not remove the default package");
						}
					}else{
						messageUnknown(sender, "Package: " + args[1] + " is not currently listed in the config.yml.");
					}
				}else{
					if(hasCommandPermission(sender, "rankup.ru")){
						messageUnknown(sender, "Incorrect sub-command, or you may not have permission to use that command.");
						displayHelp(sender);
					}
				}
			}else{
				messageUnknown(sender, "Not a valid package name.");
			}
		}else{
			if(hasCommandPermission(sender, "rankup.ru")){
				displayHelp(sender);
			}
		}
		return true;
	}
	
	private static void messageUnknown(CommandSender sender, String str){
		if(sender instanceof Player)
			Utility.messagePlayer((Player)sender, str);
		else
			Utility.printConsole(str);
	}
	
	private boolean hasCommandPermission(CommandSender sender, String path){
		if(sender instanceof Player)
			return pm.hasPermission((Player)sender, path);
		else
			return true;
	}
	
	private void displayHelp(CommandSender sender){	
		for(String ln : helpMenu){
			messageUnknown(sender, ln);
		}
		if(sender instanceof Player)
			Utility.messagePlayer((Player)sender, "/rankup <player> [package]");
		else
			Utility.printConsole("Do not inclue the /'s.");
	}
	
	private ConfigManager config;
	private PermissionsManager pm;
	
	private String[] helpMenu;

}
